package java_net_uri.url.decode;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import util.LogUtil;


public class TestDecode {
	private static final String TAG = "TestDecode";

	private static final int TIME_OUT = 5 * 1000;
	private static final int BUFFER_SIZE = 8 * 1024;

	private static final String URL = "http://api.locoso.org/v1/";
	

	public static void main(String[] argz) {
//		 isSpecUrl(url);

//		testDecode(url2);

//		// String s = getContent(url);
//		// LogUtil.e(TAG, "s:" + s);
		
		TestDecode t = new TestDecode();
		
		final String url1 = "locoso://raiyi.com/;action=login?username=111&password=222";
		final String url2 = "locoso://raiyi.com/;action=viewcompany?companyid=111";
		
		t.gotoUrl(url2);
	}
	private boolean gotoUrl(String url1) {
		try {
			URI mUrl = new URI(url1);
			if(isSpecUrlLogin(mUrl)){
				LogUtil.e(TAG, "is spec 1");
				String query = mUrl.getQuery();
				LogUtil.e(TAG, "query:" + query);
				String[] loginpair = getLoginPair(query);
				
				if(loginpair!=null){
					LogUtil.e(TAG, "username!=null && password!=null");
					startIntent(loginpair);	
				}
				return true;
			}
			
			if(isSpecUrlViewCompany(mUrl)){
				LogUtil.e(TAG, "is spec 2");	
				String query = mUrl.getQuery();
				long companyid = getCompanyId(query);
				if(companyid!=-1){
					LogUtil.e(TAG, "companyid!=-1");	
					startIntent(companyid);					
				}
				return true;
			}
			
			LogUtil.e(TAG, "not spec");
		} catch (Exception e) {
			LogUtil.e(TAG, "", e);
		}
		return false;
	}

	private static String[] getLoginPair(String query) throws Exception {
		String[] pair = query.split("&"); 
		if(pair!=null){
			if(pair.length!=2)
				return null;
			
			LogUtil.e(TAG, "param 0:" + pair[0].replace("username=", "") + ", param 1:" + pair[1].replace("password=", ""));
			return pair;
		}
		return null;
	}
	private static long getCompanyId(String query) throws Exception {
		try{
			String path = query.replace("companyid=", "");
			long id = Long.valueOf(path);
			LogUtil.e(TAG, "id:" + id);
			return id;
		} catch (Exception e){
			LogUtil.e(TAG, "", e);
		}		
		return -1;
	}
	
	private void startIntent(long companyid) {

	}

	private void startIntent(String[] loginpair) {

	}

	private static final String SCHEME = "locoso";
	private static final String HOST = "raiyi.com";
	private static boolean isSpecUrlLogin(URI uri) {
		if(uri==null)
			return false;
		final String PATH = "/;action=login";			
		if(SCHEME.equals(uri.getScheme()) && HOST.equals(uri.getHost()) && PATH.equals(uri.getPath())){
			return true;
		}
		return false;
	}
	
	private static boolean isSpecUrlViewCompany(URI uri) {
		final String PATH = "/;action=viewcompany";	
		if(SCHEME.equals(uri.getScheme()) && HOST.equals(uri.getHost()) && PATH.equals(uri.getPath())){	
			return true;
		}
		return false;
	}

	private void testDecode(String string) throws MalformedURLException,
			URISyntaxException {
		// URI mUrl=new URI (fullURL(string));
		URI mUrl = new URI(string);
		// URL mUrl=new URL(Aurl,"tutorial.intro.html#DOWNLOADING");
		System.out.println("scheme=" + mUrl.getScheme());
		System.out.println("host =" + mUrl.getHost());
		System.out.println("port=" + mUrl.getPort());
		System.out.println("path=" + mUrl.getPath());
		System.out.println("query=" + mUrl.getQuery());
		System.out.println("UserInfo=" + mUrl.getUserInfo());
		System.out.println("Authority=" + mUrl.getAuthority());
	}

	private static boolean isSpecUrl(String string)
			throws MalformedURLException {
		URL mUrl = new URL(string);
		// URL mUrl=new URL(Aurl,"tutorial.intro.html#DOWNLOADING");
		System.out.println("protocol=" + mUrl.getProtocol());
		System.out.println("host =" + mUrl.getHost());
		System.out.println("filename=" + mUrl.getFile());
		System.out.println("port=" + mUrl.getPort());
		System.out.println("ref=" + mUrl.getRef());
		System.out.println("query=" + mUrl.getQuery());
		System.out.println("path=" + mUrl.getPath());
		System.out.println("UserInfo=" + mUrl.getUserInfo());
		System.out.println("Authority=" + mUrl.getAuthority());

		return true;
	}

	private static String fullURL(String string) {
		return URL + string;
	}

	private static String getContent(String url) throws Exception {
		url = fullURL(url);
		StringBuilder sb = new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpParams httpParams = client.getParams();

		LogUtil.e(TAG, "url:" + url);

		HttpConnectionParams.setConnectionTimeout(httpParams, TIME_OUT);

		HttpResponse response = client.execute(new HttpGet(url));
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					entity.getContent(), "UTF-8"), BUFFER_SIZE);
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
				HttpConnectionParams.setConnectionTimeout(httpParams, TIME_OUT);
				LogUtil.e(TAG, client.getConnectionManager().toString());
			}
			reader.close();
		}

		String result = sb.toString();
		LogUtil.e(TAG, "getContent():" + result);

		return result;
	}
}
